//index.js
//获取应用实例
var app = getApp()
var util = require('../../utils/util.js')
var config = require('../../config.js')
var mLoadTask;
Page({
  data: {
    showModal: false,

  },
  toDetails:function(e){
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.calendar
      })
      return;
    }
    wx.navigateTo({
     url: '../calendar/calendar',
   })

  },
 // 跳转至排行榜
  toToplist: function () {
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.toplist
      })
      return;
    }
    // wx.navigateTo({
    //   url: '../toplist/toplist'
    // })
 wx.navigateTo({
      url: '../exam/demo/demo'
    })
    
  },
   // 跳转至日历
  todailylist: function (e) {
    wx.navigateTo({
      url: '../daily/daily',
    })
  },
  // 跳转至法律考试
  toExam: function (e) {
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.homeExam
      })
      return;
    }
    wx.navigateTo({
      url: '../exam/homeExam/homeExam',
    })
    // wx.navigateTo({
    //   url: '../exam/demo/demo',
    // })
  },
    // 跳转至规则详情
  toRuledetails:function(e){
    wx.navigateTo({
      url: '../rules/rules',
    })

  },
    // 跳转至我的记录
  toRuleslist:function(e){
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.myrecord
      })
      return;
    }
    wx.navigateTo({
      url: '../myrecord/myrecord',
    })

  },
  // 跳转至问题反馈
  toFeedback:function(e){
    wx.navigateTo({
      url: '../feedback/feedback',
    })

  },
  // 跳转至擂台赛
  toCompetition:function(e){
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.competition
      })
      return;
    }
    wx.navigateTo({
      url: '../games/competition/competition',
    })
  },

  toTopListShow: function(){
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.toplistshowResult
      })
      return;
    }
    wx.navigateTo({
      url: '../games/toplistshow/toplistshow',
    })

  },

  // 跳转至一战到底
  goToplay:function(e){
    if (!app.globalData.userInfo) {
      console.log("判断是否授权，否则打开授权");
      //未授权
      wx.navigateTo({
        url: '../authorization/authorization?pageId=' + config.gameInstruction
      })
      return;
    }
    wx.navigateTo({
      url: '../games/gameInstruction/gameInstruction',
    })

  },
  enter:function(){
    wx.navigateTo({
      url: '../exam/examSetup/examSetup',

    })
  },
  /**
   * 页面加载完成
   */
  onLoad: function (options) {
    var that = this;
    this.getBannerImages();
    this.loadingAnim();
    //==========分享先跳转到首页，在跳转到对应的界面============
    console.log('pageId====>',options.pageId)


    if (options.pageId == config.calendar) {
      that.toDetails();
    }
    if (options.pageId == config.gameInstruction){
      that.goToplay();
    }
    if (options.pageId == config.throughGame) {
      that.goToplay();
    }
    if (options.pageId == config.toplist) {
      that.toToplist();
    }
    if (options.pageId == config.toplistshowResult) {
      that.toDetails();
    }
    if (options.pageId == config.calendar) {
      that.toDetails();
    }
    if (options.pageId == config.homeExam) {
      that.toExam();
    }

    if (options.pageId == config.competition) {
      try {













        // 听 
        // 听听
        
        wx.setStorageSync(config.roomId, options.roomId)
        if (!app.globalData.userInfo) {
          console.log("判断是否授权，否则打开授权");
          //未授权
          wx.navigateTo({
            url: '../authorization/authorization?pageId=' + config.competition,
          })
          return;
        }
        wx.navigateTo({
          url: '../games/invitefriend/invitefriend?pageId=' + config.competition,
        })
      } catch (e) {
      }

    }
  
    if (options.pageId == config.battleResult) {
      try{
        wx.setStorageSync(config.roomId, options.roomId)
      }catch(e){
      }
      if (!app.globalData.userInfo) {
        console.log("判断是否授权，否则打开授权");
        //未授权
        wx.navigateTo({
          url: '../authorization/authorization?pageId=' + config.battleResult
        })
        return;
      }
      wx.navigateTo({
        url: '../games/battleResult/battleResult?pageId=' + config.battleResult,
      })
    }

    try {
      var isFirst = wx.getStorageSync('isFirst')
      if (isFirst == 1) {
        that.setData({
          showModal: true
        });
      }
    } catch (e) {
    }
  },

  /**
   * 监听加载动画
   * 这里不知道小程序怎么监听回调  所以我用了任务栈不断获取状态
   *if you can do it better， just do it
   * 
   */
  loadingAnim: function(){
    var that = this;
    var step;
    var animation = function () {
     
      if (app.globalData.hasLogin) {
        that.setData({
          hasLogin: true
        });
        //证明登录成功 拿到了openid 此时隐藏loading
        clearInterval(mLoadTask);
        // wx.showToast({
        //   title: '登录成功',
        //   icon: 'none',
        //   duration: 1000
        // })
      } else {
        if (step == 20){
            //20 * 500 = 10000 =10s 
          that.setData({
            hasLogin: false
          });
          wx.showToast({
            title: '登录失败，请重试',
            icon: 'none',
            duration: 2000
          })
          clearInterval(mLoadTask);
        }
      }

      step++
    };
    mLoadTask = setInterval(animation, 500);

  },
 
  getBannerImages: function(){
    var that = this;
    wx.request({
      url: config.ruleList,
      method: "POST",
      data: {
         type:1
      },
      success: function (res) {
        console.log(config.ruleList)
        if (res.data.success == 0) {
          that.setData({
            rules: res.data.rows.categoryIntro,
            bannerImg: res.data.rows.categoryImg
          });
        }
      }
    });

  },
onShow:function(){
    //app.globalData.bgMusic.play();
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '快来测测你的法律常识！',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功
        console.log(res);
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },/**
     * 弹窗
     */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  }
})

